package com.hps.boot.shiro.controller;

import com.alibaba.fastjson.JSON;
import com.hps.boot.shiro.model.User;
import com.hps.boot.shiro.util.JwtUtil;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heps
 * @since 2020/6/9 18:17
 */
@RestController
@RequestMapping("/user")
public class UserController {

  @RequestMapping("/login")
  public void login(User user, HttpServletResponse response) throws IOException {
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
    response.setContentType("application/json;charset=utf-8");
    Map<String, Object> result = new HashMap<>();
    String jwt =  JwtUtil.sign(1L);
    result.put("token", jwt);
    response.setHeader("Authorization", "Bearer " + jwt);
    response.getWriter().write(JSON.toJSONString(result));
  }
}
